﻿using System;
using System.Reflection;
using System.Linq;

namespace Alma.Library.Helper
{
    public sealed class EnumHelper
    {
        private static Type attrType = typeof(EnumDescriptorAttribute);

        private EnumHelper() { }

        public static TEnum[] GetEnumValues<TEnum>()
        {
            var type = typeof(TEnum);
            if (!type.IsEnum)
            {
                throw new ArgumentException("类型 '" + type.Name + "' 不是枚举类型。");
            }
            return (
              from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
              where field.IsLiteral
              select (TEnum)field.GetValue(type)).ToArray();
        }

        public static string[] GetEnumNames<TEnum>()
        {
            var type = typeof(TEnum);
            if (!type.IsEnum)
                throw new ArgumentException("类型 '" + type.Name + "' 不是枚举类型。");

            return (
              from field in type.GetFields(BindingFlags.Public | BindingFlags.Static)
              where field.IsLiteral
              select field.Name).ToArray();
        }
    }
}
